package com.heaven1949.tmall.common.web.config;

import ch.qos.logback.classic.pattern.MessageConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import cn.hutool.json.JSONUtil;
import org.slf4j.helpers.MessageFormatter;

import java.util.stream.Stream;

/**
 * logback参数打印JSON格式化类
 *
 * @author Hollis
 */
public class ArgumentJsonFormatLayout extends MessageConverter {

    @Override
    public String convert(ILoggingEvent event) {
        try {
            return MessageFormatter.arrayFormat(event.getMessage(),
                    Stream.of(event.getArgumentArray())
                            .map(e -> JSONUtil.toJsonStr(e))
                            .toArray()).getMessage();
        } catch (Exception e) {
            return event.getMessage();
        }
    }
}